package cn.tedu.truetaste.service.impl;

import cn.tedu.truetaste.mapper.DishesMapper;
import cn.tedu.truetaste.pojo.entity.DishesForElastic;
import cn.tedu.truetaste.pojo.entity.Spu;
import cn.tedu.truetaste.pojo.entity.SpuForElastic;
import cn.tedu.truetaste.repository.SpuForElasticRepository;
import cn.tedu.truetaste.restful.JsonPage;
import cn.tedu.truetaste.service.ISearchService;
import cn.tedu.truetaste.service.ISpuService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SearchServiceImpl implements ISearchService {
    @Autowired
    private ISpuService spuService;
    @Autowired
    private DishesMapper dishesMapper;
    @Autowired
    private SpuForElasticRepository spuRepository;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public void loadSpuByPage() {
        int i=1;
        int page;
        do {
            JsonPage<Spu> spus=spuService.getSpuByPage(i,2);
            List<SpuForElastic> esSpus=new ArrayList<>();
            for (Spu spu : spus.getList()){
                SpuForElastic esSpu=new SpuForElastic();
                BeanUtils.copyProperties(spu,esSpu);
                esSpus.add(esSpu);
            }
            spuRepository.saveAll(esSpus);
            log.info("信息加载成功！第{}页",i);
            i++;
            page=spus.getTotalPage();
        }while (i<=page);
    }

    @Override
    public JsonPage<DishesForElastic> search
            (String keyword, Integer page, Integer pageSize) {

        Pageable p = PageRequest.of(page-1,pageSize);

//        Page<SpuForElastic> spus=spuRepository.
//                querySearch(keyword, PageRequest.of(page-1,pageSize));
        IndexCoordinates index = IndexCoordinates.of("zpw_dc");
        Query searchQuery = new NativeSearchQueryBuilder()
                // 添加查询条件
                .withQuery(QueryBuilders.matchQuery("name", keyword))
                .withPageable(p)
                .build();
        SearchHits<DishesForElastic> search = elasticsearchRestTemplate.search(searchQuery, DishesForElastic.class, index);
        List<SearchHit<DishesForElastic>> searchHits = search.getSearchHits();
        List<DishesForElastic> list = new ArrayList<>();
        for(SearchHit<DishesForElastic> searchHit : searchHits){
            DishesForElastic content = searchHit.getContent();
            list.add(content);
            System.out.println(content);
        }
        Pageable spus = searchQuery.getPageable();
        JsonPage<DishesForElastic> jsonPage=new JsonPage<>();
        jsonPage.setPage(page);
        jsonPage.setPageSize(pageSize);
//        jsonPage.setTotal(spus.get);
//        jsonPage.setTotalPage(spus.getTotalPages());
        jsonPage.setList(list);

        return jsonPage;
    }


    /**
     * 数据库查询后批量新增数据
     */
    @Override
    public void testAddAllDoc(){
        // 查询数据库的所有spu数据
        List<DishesForElastic> hotels = dishesMapper.findAllList();

        // 执行批量保存业务
        Iterable<DishesForElastic> save = elasticsearchRestTemplate.save(hotels);
    }

}
